//
//  MineInfoViewController.m
//  MJApp
//
//  Created by scjh on 15/3/27.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoViewController.h"

#import "MineInfoImageTableViewCell.h"
#import "MineInfoDescTableViewCell.h"

#import "MineInfoEditViewController.h"
#import "MinePhotosViewController.h"

#import "NSDictionary+Category.h"


@interface MineInfoViewController ()<UITableViewDataSource,UITableViewDelegate,HomeCellDelegate>

@property(nonatomic,strong)UICollectionView *mineCollectionView;

@property(nonatomic,strong)UITableView *mineTableView;

@property(nonatomic,strong)NSDictionary *userInfoDic;
@property(nonatomic,strong)NSArray *photos;

@end

@implementation MineInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"个人中心";
    [self UI];
    [self NaviUI];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self obtaionData];
//    [((TabBarController *)self.tabBarController) setTabbarHidden:YES];
}

- (void)obtaionData {
    
    NSDictionary *dic = @{@"user_id":[userInfoTool share].ID};
    [WLHttpRequestAfter userCenterInfoWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            self.userInfoDic = [resultDictionary[@"data"][@"user"] nullValueToEmpty];
            self.photos = resultDictionary[@"data"][@"photo"];
            [self.mineTableView reloadData];
        }else {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)UI {
    
    
    CGRect bounds = self.view.bounds;
    bounds.size.height = IOS_IPHONE_WINDOW_HIGH;
    bounds.size.width = IOS_IPHONE_WINDOW_WIDE;
    self.mineTableView = [[UITableView alloc] initWithFrame:bounds style:UITableViewStylePlain];
    self.mineTableView.delegate = self;
    self.mineTableView.dataSource = self;
    self.mineTableView.separatorColor = [UIColor clearColor];
    self.mineTableView.backgroundColor = Color_Back_Black;
    [self.view addSubview:self.mineTableView];
}

- (void)NaviUI {
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [rightButton setTitle:@"编辑" forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(editAction:)
          forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)editAction:(UIButton *)sender {
    
    [self.navigationController
     pushViewController:[[MineInfoEditViewController alloc] initWithDic:self.userInfoDic]
     animated:YES];
}

#pragma mark - tableView delegate 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.userInfoDic) {
        
        return 2;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        MineInfoImageTableViewCell *cell = (MineInfoImageTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;
    }else {
        
        MineInfoDescTableViewCell *cell = (MineInfoDescTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        MineInfoImageTableViewCell *cell = [MineInfoImageTableViewCell initWithTableView:tableView];
        cell.delegate = self;
        cell.photos = self.photos;
        return cell;
    }else {
        
        MineInfoDescTableViewCell *cell = [MineInfoDescTableViewCell initWithTableView:tableView];
        cell.infoDic = self.userInfoDic;
        return cell;
    }
}

#pragma mark homeCellDelegate
- (void)homeCell:(id)cell withInfo:(id)info {
    
    MinePhotosViewController *minePhotoVC = [[MinePhotosViewController alloc] initWithOriginArray:self.photos];
    [self.navigationController pushViewController:minePhotoVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
